﻿using Microsoft.EntityFrameworkCore;
using My.Domain.Comments;
using My.Domain.IssueLabels;
using My.Domain.IssueRepositorys;
using My.Domain.Issues;
using My.Domain.Labels;
using My.Domain.Orders;
using My.Domain.Shared.Comments;
using My.Domain.Shared.IssueRepositorys;
using My.Domain.Shared.Issues;
using My.Domain.Shared.Labels;
using My.Domain.Shared.Orders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace My.EntityFrameworkCore.EntityFrameworkCore
{
    [ConnectionStringName("Default")]
    public class DefaultDbContext : AbpDbContext<DefaultDbContext>
    {
        public DbSet<Order> Orders { get; set; }

        public DbSet<Comment> Comments { get; set; }

        public DbSet<IssueLabel> IssueLabels { get; set; }

        public DbSet<IssueRepository> IssueRepositories { get; set; }

        public DbSet<Issue> Issues { get; set; }

        public DbSet<Label> Labels { get; set; }

        //public DbSet<OrderLine> OrderLines { get; set; }

        public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.ConfigureAuditLogging();

            modelBuilder.Entity<Order>(build =>
            {
                build.ToTable(nameof(Order)).HasKey(x => x.Id);
                build.Property(x => x.ReferenceNo).HasColumnType($"varchar({OrderConsts.ReferenceNoLength})");

                build.HasMany(x => x.OrderLines).WithOne().HasForeignKey(x => x.OrderId);
            });

            modelBuilder.Entity<OrderLine>(build =>
            {
                build.ToTable(nameof(OrderLine)).HasKey(x => new { x.OrderId, x.ProductId });
            });

            modelBuilder.Entity<Comment>(build =>
            {
                build.ToTable(nameof(Comment)).HasKey(x => x.Id);
                build.Property(x => x.Text).HasMaxLength(CommentConsts.TextLength);
                build.Property(x => x.CreationTime).HasColumnType("datetime");
            });

            modelBuilder.Entity<IssueLabel>(build =>
            {
                build.ToTable(nameof(IssueLabel)).HasKey(x => new { x.IssueId, x.LabelId });
            });

            modelBuilder.Entity<IssueRepository>(build =>
            {
                build.ToTable(nameof(IssueRepository)).HasKey(x => x.Id);
                build.Property(x => x.Name).HasMaxLength(IssueRepositoryConsts.NameLength);
            });

            modelBuilder.Entity<Issue>(build =>
            {
                build.ToTable(nameof(Issue)).HasKey(x => x.Id);
                build.Property(x => x.Title).HasMaxLength(IssueConsts.TitleLength);
                build.Property(x => x.Text).HasMaxLength(IssueConsts.TextLength);

                build.HasMany(x => x.Comments).WithOne().HasForeignKey(x => x.IssueId);
                build.HasMany(x => x.IssueLabels).WithOne().HasForeignKey(x => x.LabelId);
                build.Property(x => x.CreationTime).HasColumnType("datetime").HasDefaultValue("2023-01-29");
                build.Property(x => x.LastCommentTime).HasColumnType("datetime");
            });

            modelBuilder.Entity<Label>(build =>
            {
                build.ToTable(nameof(Label)).HasKey(x => x.Id);
                build.Property(x => x.Name).HasMaxLength(LabelConsts.NameLength);
                build.Property(x => x.Color).HasMaxLength(LabelConsts.ColorLength);
            });
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
        }
    }
}
